﻿#region MIT

//  /*The MIT License (MIT)
// 
//  Copyright 2016 lizs lizs4ever@163.com
//  
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//  
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//  
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//   * */

#endregion

using System;

namespace mom
{
    /// <summary>
    ///     基于mom逻辑服务的定时器
    /// </summary>
    public class TimerWrapper
    {
        /// <summary>
        ///     创建定时器
        /// </summary>
        /// <param name="action"></param>
        /// <param name="delay"></param>
        /// <returns></returns>
        public static TimerWrapper Create(Action action, uint delay)
        {
            return new TimerWrapper(action, delay);
        }

        /// <summary>
        ///     创建定时器
        /// </summary>
        /// <param name="action"></param>
        /// <param name="delay"></param>
        /// <param name="period"></param>
        /// <returns></returns>
        public static TimerWrapper Create(Action action, uint delay, uint period)
        {
            return new TimerWrapper(action, delay, period);
        }

        private readonly Timer _timer;
        private readonly Action _action;

        /// <summary>
        ///     延迟
        /// </summary>
        public uint Delay { get; }

        /// <summary>
        ///     周期
        /// </summary>
        public uint? Period { get; }

        /// <summary>
        ///     开始时间戳
        /// </summary>
        public long StartTime { get; private set; }

        /// <summary>
        ///     可曾执行过
        /// </summary>
        public bool Invoked { get; private set; }

        /// <summary>
        ///     开始至今流逝时间
        /// </summary>
        public long Elapsed => Loop.Ins.ElapsedMilliseconds - StartTime;

        /// <summary>
        ///     是否需要重复调度
        /// </summary>
        public bool Repeated => Period.HasValue;

        /// <summary>
        ///     启动
        /// </summary>
        public void Start()
        {
            _timer.Start();
            StartTime = Loop.Ins.ElapsedMilliseconds;
        }

        /// <summary>
        ///     停止
        /// </summary>
        public void Stop()
        {
            _timer.Arrived -= OnTimer;
            _timer.Stop();
        }

        private void OnTimer(Timer timer)
        {
            _action();
            Invoked = true;
        }

        private TimerWrapper(Action action, uint delay)
        {
            _timer = new Timer(delay);
            _timer.Arrived += OnTimer;
            _action = action;

            Delay = delay;
            Period = null;
        }

        private TimerWrapper(Action action, uint delay, uint period)
        {
            _timer = new Timer(delay, period);
            _timer.Arrived += OnTimer;
            _action = action;

            Delay = delay;
            Period = period;
        }
    }
}
